package com.sgb.knowledgeshare.weight;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;

import com.github.jdsjlzx.ItemDecoration.GridItemDecoration;
import com.github.jdsjlzx.interfaces.OnItemClickListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.sgb.knowledgeshare.R;
import com.sgb.knowledgeshare.entity.VideoFileBean;
import com.sgb.knowledgeshare.utils.RxBus;
import com.sgb.knowledgeshare.utils.ShareFileUtils;
import com.sgb.knowledgeshare.view.activity.ShareVideoCompileActivity;
import com.sgb.knowledgeshare.view.activity.ShareVideoInfoActivity;
import com.sgb.knowledgeshare.view.adapter.VideoPhotoAdapter;
import com.swgk.core.dialog.RoundCornerDialog;

import java.util.List;

import rx.functions.Action1;

/**
 * Created by：冯齐 on 2020/11/20 18:03
 * Description： 分享-发布-相册选择
 */
public class ShareVideoPhotoDialog {

    private final LRecyclerView videoLists;
    private final TextView affirm;
    private final RelativeLayout selectPhotoClose;
    private final LinearLayout rlOk;
    private final LinearLayout rlNodeta;
    private LRecyclerViewAdapter mLRecyclerViewAdapter;
    public final List<VideoFileBean> videoFileList;
    private int positions = -1;
    private final Dialog dialog;

    public ShareVideoPhotoDialog(Activity context) {
        //1、使用Dialog、设置style
        dialog = new Dialog(context, R.style.RoundCornerDialog);
        //2、设置布局
        View view = View.inflate(context, R.layout.share_video_photo, null);
        videoLists = view.findViewById(R.id.videoList);
        selectPhotoClose = view.findViewById(R.id.select_photo_close);
        rlNodeta = view.findViewById(R.id.rl_nodeta);
        rlOk = view.findViewById(R.id.rl_ok);
        affirm = view.findViewById(R.id.affirm);
        affirm.setClickable(false);
        dialog.setContentView(view);
        Window window = dialog.getWindow();
        //设置弹出位置
        window.setGravity(Gravity.BOTTOM);
        //设置弹出动画
        window.setWindowAnimations(R.style.ActionSheetDialogAnimation);
        //设置对话框大小
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        dialog.show();
        selectPhotoClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        //  Log.e("tag视频路径信息",FileUtils.getVideoFileList(context).toString());
        videoFileList = ShareFileUtils.getVideoFileList(context);
        if (videoFileList!=null&&videoFileList.size() > 0) {//显示记载数据
            videoLists.setVisibility(View.VISIBLE);
            rlOk.setVisibility(View.VISIBLE);
            rlNodeta.setVisibility(View.GONE);
            initList(context, videoFileList);
        } else {//无数据时展示无数据布局
            videoLists.setVisibility(View.GONE);
            rlOk.setVisibility(View.GONE);
            rlNodeta.setVisibility(View.VISIBLE);
        }
        affirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (videoFileList!=null&&videoFileList.size()>0){
                    for (VideoFileBean at:videoFileList) {
                        if (at.isSelect()) {
                            long size = at.getSize();
                            if (size > 300000) {
                                showDialog(context, 1);
                                return;
                            }
                            if (size < 6000) {
                                showDialog(context, 2);
                                return;
                            }
                            //跳转到到编辑视频界面
                            Intent intent = new Intent(context, ShareVideoCompileActivity.class);
                            intent.putExtra("videoPath", at.getPath());
                            context.startActivity(intent);
                        }
                    }
                }
            }
        });
        RxBus.getDefault().toObservable(VideoFileBean.class).subscribe((Action1<VideoFileBean>) fileBean -> {
            for (int i = 0; i < videoFileList.size(); i++) {
                VideoFileBean bean = videoFileList.get(i);
                if (bean.getPath().equals(fileBean.getPath())) {
                    bean.setSelect(fileBean.isSelect());
                    if (bean.isSelect()) {
                        affirm.setClickable(true);
                        affirm.setTextColor(context.getResources().getColor(R.color.white));
                        affirm.setBackgroundResource(R.mipmap.share_photo_ok);
                    } else {
                        affirm.setClickable(false);
                        affirm.setTextColor(context.getResources().getColor(R.color.color_969696));
                        affirm.setBackground(context.getResources().getDrawable(R.drawable.share_gray_ok));
                    }
                } else {
                    if (fileBean.isSelect()) {
                        bean.setSelect(false);
                    }
                }
            }
            mLRecyclerViewAdapter.notifyDataSetChanged();
        });
    }

    private void initList(Activity context, List<VideoFileBean> videoFileList) {
        VideoPhotoAdapter videoPhotoAdapter = new VideoPhotoAdapter(context, videoFileList);
        mLRecyclerViewAdapter = new LRecyclerViewAdapter(videoPhotoAdapter);
        com.github.jdsjlzx.ItemDecoration.GridItemDecoration divider = new GridItemDecoration.Builder(context)
                .setHorizontal(R.dimen.dp3)
                .setVertical(R.dimen.dp3)
                .setColorResource(R.color.white)
                .build();
        videoLists.addItemDecoration(divider);
        GridLayoutManager layoutManager = new GridLayoutManager(context, 3);
        videoLists.setLayoutManager(layoutManager);
        videoLists.setAdapter(mLRecyclerViewAdapter);
        mLRecyclerViewAdapter.removeHeaderView();
        mLRecyclerViewAdapter.removeFooterView();
        videoLists.setPullRefreshEnabled(false);
        videoLists.setLoadMoreEnabled(false);
        mLRecyclerViewAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(context, ShareVideoInfoActivity.class);
                intent.putExtra("bean", videoFileList.get(position));
                context.startActivity(intent);
            }
        });
        videoPhotoAdapter.setOnSelectVideoClick(new VideoPhotoAdapter.OnSelectVideoClick() {
            @Override
            public void getOnSelectVideoClick(int pos) {
                boolean isSelected = !videoFileList.get(pos).isSelect();
                for (VideoFileBean data : videoFileList) {
                    data.setSelect(false);
                }
                videoFileList.get(pos).setSelect(isSelected);
                mLRecyclerViewAdapter.notifyDataSetChanged();
                if (isSelected) {
                    positions = pos;
                    affirm.setClickable(true);
                    affirm.setTextColor(context.getResources().getColor(R.color.white));
                    affirm.setBackgroundResource(R.mipmap.share_photo_ok);
                } else {
                    affirm.setClickable(false);
                    affirm.setTextColor(context.getResources().getColor(R.color.color_969696));
                    affirm.setBackground(context.getResources().getDrawable(R.drawable.share_gray_ok));
                }
            }
        });

    }


    //提示弹窗
    private void showDialog(Activity context, int i) {
        View view = View.inflate(context, R.layout.dialog_prompt_btn, null);
        final RoundCornerDialog roundCornerDialog = new RoundCornerDialog(context, 0, 0, view, R.style.RoundCornerDialog);
        roundCornerDialog.show();
        roundCornerDialog.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失
        TextView tv_title = (TextView) view.findViewById(R.id.tv_title);
        TextView tv_message = (TextView) view.findViewById(R.id.tv_message);
        tv_title.setVisibility(View.VISIBLE);
        tv_title.setText("温馨提示");
        TextView tv_logout_confirm = (TextView) view.findViewById(R.id.tv_logout_confirm);
        tv_logout_confirm.setText("我知道了");
        tv_logout_confirm.setTextColor(context.getResources().getColor(R.color.color_5792FD));
        tv_message.setTextColor(context.getResources().getColor(R.color.color_tab_two));
        if (i == 1) {
            tv_message.setText("不能上传超过5分钟的视频");
        } else {
            tv_message.setText("视频时长不得小于6s");
        }

        //确定
        tv_logout_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                roundCornerDialog.dismiss();
            }
        });
    }


    public void finishDialog() {
        if (dialog != null) {
            dialog.dismiss();
        }
    }
}
